package com.czl.module_service.viewmodel.inventory

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.InventoryAssetCountBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
class InventoryViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var execId : String? = null
    var taskId : Long? = null
    var taskName = ObservableField("")
    var locationDesc = ObservableField("")
    val tabData = ObservableField("")
    val uc = UiChangeEvent()
    var status: Int? = null
    var titleName = ObservableField("")

    inner class UiChangeEvent {
        val tabChangeLiveEvent: SingleLiveEvent<Int> = SingleLiveEvent()
        val pageChangeLiveEvent: SingleLiveEvent<Int> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<InventoryAssetCountBean> = SingleLiveEvent()
        val loadErrorEvent: SingleLiveEvent<Unit?> = SingleLiveEvent()
    }

    val onPageSelectedListener: BindingCommand<Int> = BindingCommand(BindingConsumer {
        status = it
        uc.pageChangeLiveEvent.postValue(it)
    })

    val onTabSelectedCommand: BindingCommand<Int> = BindingCommand(BindingConsumer {
        uc.tabChangeLiveEvent.postValue(it)
    })

    val search: BindingCommand<Void> = BindingCommand(BindingAction {
        LiveBusCenter.postSearchParamReturnEvent(titleName.get())
        /*showLoading()
        wechatGetTaskFacilityInfoCountByTaskId(execId = execId, taskId = taskId, titleName = titleName.get())*/
    })

    fun wechatGetTaskFacilityInfoCountByTaskId(execId: String?, taskId: Long?, titleName: String?) {
        model.mTengYunHttpDataSource
            .wechatGetTaskFacilityInfoCountByTaskId(
                GsonUtils.toJson(
                    mapOf(
                        "execId" to execId,
                        "status" to status,
                        "taskId" to taskId,
                        "facilityName" to titleName
                    )
                )
            )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<InventoryAssetCountBean>>() {
                override fun onResult(t: BaseBean<InventoryAssetCountBean>) {
                    if (t.state == 0) {
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadErrorEvent.postValue(null)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    uc.loadErrorEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }
}